<template>
    <div class="app-container">

        <div class="searchAll" slot="search">
            <el-form :model="queryParams" ref="queryForm" label-position="right" v-show="showSearch" label-width="68px">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-form-item :label="$t('logginLog.IP')" prop="ipaddr">
                            <el-input v-model="queryParams.ipaddr" :placeholder="$t('public.placeholder',{placeholder:$t('logginLog.IP')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('logginLog.userName')" prop="userName">
                            <el-input v-model="queryParams.userName" :placeholder="$t('public.placeholder',{placeholder:$t('logginLog.userName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('public.status')" prop="status">
                            <el-select style="width: 100%" v-model="queryParams.status" :placeholder="$t('public.placeholder',{placeholder:$t('logginLog.status')})" clearable size="small">
                                <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('logginLog.loginTime')">
                            <el-date-picker v-model="dateRange" size="small" style="width: 100%" value-format="yyyy-MM-dd" type="daterange" autocomplete="off" range-separator="-" :start-placeholder="$t('public.startDate')" :end-placeholder="$t('public.endDate')"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <div class="searchAres ">
                            <el-button type="primary" icon="el-icon-search" size="mini" class="searchBtn" @click="handleQuery">{{ $t("public.query") }}</el-button>
                            <el-button class="resectBtn" icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("public.reset") }}</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="tableList">
            <div class="insTitle">
                <span>{{ $t("systemTitle.LoginLog") }}</span>
            </div>
            <div class="btnGroup">
                <el-button v-hasPermi="['tenant:logininfor:remove']" type="primary" icon="el-icon-delete" size="mini" class="additemBtn" @click="handleClean">{{ $t("public.clear") }}</el-button>
                <el-button v-hasPermi="['tenant:logininfor:remove']" icon="el-icon-delete" size="mini" class="deleteitemBtn" :disabled="multiple" @click="handleDelete">{{ $t("public.delete") }}</el-button>
                <el-button v-hasPermi="['tenant:logininfor:export']" class="exportitemBtn" size="mini" :loading="exportLoading" @click="handleExport"><img src="../../../assets/images/icons/daochu.png" alt=""> {{ $t("public.export") }}</el-button>

            </div>
            <el-table height="calc(100vh - 368px)" border ref="tables" stripe v-loading="loading" :data="list" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('logginLog.visitCode')" align="center" prop="infoId" />
                <el-table-column :label="$t('logginLog.userName')" align="center" prop="userName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" />
                <el-table-column :label="$t('logginLog.IP')" align="center" prop="ipaddr" width="130" :show-overflow-tooltip="true" />

                <el-table-column :label="$t('logginLog.description')" align="center" prop="msg" />
                <el-table-column :label="$t('logginLog.visitTime')" align="center" prop="accessTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.accessTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('logginLog.status')" align="center" fixed="right" width="100" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
        </div>

    </div>
</template>

<script>
import { list, delLogininfor, cleanLogininfor } from '@/api/system/logininfor'

export default {
    name: 'Logininfor',
    dicts: ['sys_common_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            list: [],
            // 日期范围
            dateRange: [],
            // 默认排序
            defaultSort: { prop: 'loginTime', order: 'descending' },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                ipaddr: undefined,
                userName: undefined,
                status: undefined,
            },
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询登录日志列表 */
        getList() {
            this.loading = true
            list(this.addDateRange(this.queryParams, this.dateRange)).then(
                (response) => {
                    this.list = response.rows
                    this.total = response.total
                    this.loading = false
                }
            )
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.$refs.tables.sort(
                this.defaultSort.prop,
                this.defaultSort.order
            )
            this.handleQuery()
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.infoId)
            this.multiple = !selection.length
        },
        /** 排序触发事件 */
        handleSortChange(column, prop, order) {
            this.queryParams.orderByColumn = column.prop
            this.queryParams.isAsc = column.order
            this.getList()
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const infoIds = row.infoId || this.ids
            this.$modal
                .confirm(
                    this.$t('public.preConfirm', {
                        key: this.$t('logginLog.visitCode'),
                        value: infoIds,
                    })
                )
                .then(function () {
                    return delLogininfor(infoIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('operaLog.delSuc'))
                })
                .catch(() => {})
        },
        /** 清空按钮操作 */
        handleClean() {
            this.$modal
                .confirm(this.$t('logginLog.promptTip'))
                .then(function () {
                    return cleanLogininfor()
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('operaLog.clearSuc'))
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'system/saas/logininfor/export',
                {
                    ...this.queryParams,
                },
                `logininfor_${new Date().getTime()}.xlsx`
            )
        },
    },
}
</script>

